<?php

namespace mz\ars;

use Yii;
use yii\data\Pagination;

class Links extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return '{{%links}}';
    }

    public function rules()
    {
        return [
            [['url','title'], 'required'],
            [['desc','picture'], 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'picture' => 'Picture',
            'url' => 'Url',
            'desc' => 'Desc',
        ];
    }

    # 获取所有友链
    public static function getAllLinks($map = [])
    {
        $query = static::find();
        if(isset($map['title'])){
            $query->andFilterWhere(['like','title',$map['title']]);
        }
        $pageSize = isset($map['pageSize']) ?$map['pageSize']:10;
        $countQuery = clone $query;
        $pages = new Pagination([
            'pageSize' => $pageSize,
            'totalCount' => $countQuery->count(),
        ]);
        $query->orderBy("last_change desc");
        $items = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->asArray()
            ->all();
        return compact('items','pages');
    }

    # 获取友链详情
    public static function getFindOne($id)
    {
        $query = static::find()->where(['id' => $id])->asArray()->one();
        return $query;
    }

}
